﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaskManage.Models;

namespace TaskManage.Controllers
{
    public class HomeController : Controller
    {
        Model1 model = new Model1();

        // 主页
        public ActionResult Index()
        {
            var list = model.Task.ToList();
            return View(list);
        }
        // 添加
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                // 与用户交互的属性，直接使用传递的数据；
                // 特殊处理：未与用户交互，且默认值无法满足数据库要求的属性必须重新赋值（数据库默认值）。
                task.AddTime = DateTime.Now;
                task.TaskState = 1;
                task.FinishedTime = null;
                model.Task.Add(task);
                var res = model.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(task);
        }
        // 修改
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var list = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (list == null)
            {
                return RedirectToAction("Index");
            }
            return View(list);
        }
        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)//验证
            {
                var temp = model.Task.FirstOrDefault(p => p.TaskID == task.TaskID);
                if (temp == null)
                {
                    return RedirectToAction("Index");
                }
                temp.TaskPriority = task.TaskPriority;
                temp.Content = task.Content;
                temp.TaskState = task.TaskState;
                temp.FinishedTime = task.FinishedTime;
                temp.uid = task.uid;
                var result = model.SaveChanges();//保存
                if (result > 0)
                {
                    return RedirectToAction("index");
                }
            }
            return View(task);
        }
        // 详情
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var list = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (list == null)
            {
                return RedirectToAction("Index");
            }
            return View(list);
        }
        // 删除
        [HttpPost]
        public ActionResult Delete(int? id)
        {
            if (id == null)// 判断id是否为空
            {
                return Json(false);
            }
            var list = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (list == null) // 判断是否查询到数据
            {
                return Json(false);
            }
            model.Task.Remove(list); // 执行删除 
            var result = model.SaveChanges(); // 保存至数据库
            return Json(result > 0); //返回
        }
    }
}